﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AncientLand.Service.Entity;
using AncientLand.Service.IBiz;

namespace AncientLand.Controllers
{
    public class PictorialController : Controller
    {

        private readonly IPictorialCategoryBiz _pictorialCategoryService;

        public PictorialController(IPictorialCategoryBiz pictorialCategoryService)
        {
            this._pictorialCategoryService = pictorialCategoryService;
        }

        public ActionResult List(int? page=1)
        {
           
            var size = Convert.ToInt16(System.Configuration.ConfigurationSettings.AppSettings["pageSize"]);

            var totalRecord = 0;
            var categorys = _pictorialCategoryService.GetCategoryByPages(out totalRecord, pageIndex: page.GetValueOrDefault(), pageSize: size, orderFiled: "");

            var item = new PageItem()
            {
                CurrentPage = page.GetValueOrDefault(),
                TotalRecords = totalRecord,
                TotalPages = (totalRecord % size == 0) ? totalRecord / size : totalRecord / size + 1,
                ControllerName = "Pictorial",
                ActionName = "List"
            };

            var viewModels = new Pager<PictorialCategory>() { DataSource = categorys, Item = item };

            return View(viewModels);
        }


        public ActionResult Category(int id, string name)
        {
            //ViewBag.CategoryId = id;
           var category= _pictorialCategoryService.GetById(id);
           ViewBag.ThumPath = category.ThumbnailPath;
            List<string> model = GetModel("~/UpFiles/写真/", name);
            return View(model);
        }


        [ChildActionOnly]
        //[OutputCache(Duration = 3600)]
        public PartialViewResult _category(string photoType)
        {
            ViewBag.PhotoType = photoType;

            return PartialView("_category", _pictorialCategoryService.GetAll());
        }

        private List<string> GetModel(string xPath, string category)
        {
            if (string.IsNullOrEmpty(category))
            {
                System.IO.DirectoryInfo pictorialInfo = new DirectoryInfo(Server.MapPath(xPath));
                if (pictorialInfo.Exists)
                {
                    DirectoryInfo[] folders = pictorialInfo.GetDirectories();
                    if (folders.Count() > 0)
                    {
                        category = folders[0].Name;
                    }
                }
            }
            category = Server.UrlDecode(category);

            string filePath = xPath + category + "/";
            List<string> model = new List<string>();
            ViewBag.Category = category;
            ViewBag.Path = filePath;
            System.IO.DirectoryInfo directoryInfo = new DirectoryInfo(Server.MapPath(filePath));
            if (directoryInfo.Exists)
            {
                FileInfo[] fileInfos = directoryInfo.GetFiles();
                if (fileInfos.Count() > 0)
                {
                    foreach (FileInfo fileInfo in fileInfos)
                    {
                        model.Add(fileInfo.Name);
                    }
                }
            }
            return model;
        }
    }
}
